﻿<html>

	<head>
		<title>自定义分类管理</title>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">

		<link href="../../css/skin/default/style.css" rel="stylesheet" type="text/css" />
		<link href="../../css/dialog.css" rel="stylesheet">
		<link href="../../js/lib/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />

		<script src="../../js/lib/jquery/jquery-1.10.2.min.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/ligerui.all.js" type="text/javascript"></script>

		<script src="../../js/lib/ligerUI/js/plugins/ligerPopupEdit.js"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerLayout.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerGrid.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerForm.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerCheckBox.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerComboBox.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerDateEditor.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerRadio.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerTextBox.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerSpinner.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerTree.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerDialog.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerDrag.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerResizable.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerTip.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerMenu.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerToolBar.js" type="text/javascript"></script>
		<script src="../../js/lib/jquery.form.js" type="text/javascript"></script>
		<script src="../../js/lib/json2.js" type="text/javascript"></script>
		<script src="../../js/dialog.js"></script>
		<script src="../../js/JS/layout.js"></script>
		<script src="../../js/constants.js" type="text/javascript"></script>
		<script src="../../js/api.js" type="text/javascript"></script>

		<style type="text/css">
			.tree {
				width: 90%;
				height: 100%;
				border: 1px solid #d1d1d1;
				overflow: auto;
			}
		</style>
		<script type="text/javascript">
			var manager;
			var menu;
			var selectTreeRow;
			var ptid = $api.getStorage("ptype_" + $api.getStorage("user").Fsid);
			var mid = $api.getparastr("mid");
			var typeUrl = BaseUrl + "ProductType?token=" + $api.getStorage("token");
			var productUrl = BaseUrl + "ProductTypeProduct?token=" + $api.getStorage("token");

			function stop() {
				return false;
			}
			$(function() {
				//初始化
				$(document).hover(parent.hindMenu);
				document.oncontextmenu = stop;
				document.body.oncontextmenu = function rightClick() {
					window.event.returnValue = false;
				}
				//////////////////////////////////////////
				$("form").ligerForm();
				///高度//////////////////////////////////////
				var h = document.documentElement.clientHeight || document.body.clientHeight;
				$(".tree").css("height", h * 0.75);
				$("#treediv").css("height", h * 0.75);

				if($api.isEmpty(ptid)) {
					ptid = 0;
				}
				////////////////////////////////////////////
				toolbar();
				initTree();
				initGird(h * 0.75 - 18);

				manager = $("#maingrid").ligerGetGridManager();

			});
			///////////////////////////////////////////////////////////////////
			function initTree() {
				menu = $.ligerMenu({
					top: 100,
					left: 100,
					width: 140,
					items: [{
							text: '增加子级',
							click: itemclick
						},
						{
							line: true
						},
						{
							text: '修改',
							click: itemclick
						},
						{
							line: true
						},
						{
							text: '删除',
							click: itemclick
						}
					],
					isHind: true
				});

				var url = typeUrl;
				$("#treediv").ligerTree({
					onContextmenu: function(node, e) {
						selectTreeRow = node.data;

						menu.show({
							top: e.pageY,
							left: e.pageX
						});
						return false;
					},
					onSelect: function(node, e) {
						if(node)
							treeClick(node);
						return false;
					},
					nodeDraggable: false,
					checkbox: false,
					idFieldName: 'id',
					isExpantrue: false,
					nodeWidth: 235,
					ajaxType: 'get',
					url: typeUrl,
					slide: false
				});

			}

			function itemclick(item, i) {
				if('增加子级' == item.text) {
					if(!selectTreeRow) return;
					doType('addchild');
				} else if('修改' == item.text) {
					if(!selectTreeRow) return;
					if(selectTreeRow.level == -1) {
						$.ligerDialog.warn("系统分类不能修改！");
						return;
					}
					doType('edit');
				} else if('删除' == item.text) {
					if(!selectTreeRow) return;
					if(selectTreeRow.level == -1) {
						$.ligerDialog.warn("系统分类不能删除！");
						return;
					}
					if(selectTreeRow.children.length > 0) {
						$.ligerDialog.warn("请删除下级分类！");
						return;
					}
					$.ligerDialog.confirm("确定要删除分类吗?", function(yes) {
						if(yes) {
							doType('del');
						}
					})

				}
			}
			var commonDialog;

			function doType(cmd) {
				if(cmd == 'del') {
					var url = typeUrl + "&action=" + cmd;
					var param = {
						"Fid": selectTreeRow.id
					}
					$api.ajaxapp2(url,  param  , function(data) {
						initTree();
						f_reload();
					});
					return;
				}
				if("edit" == cmd)
					commonDialog = parent.openCommWindow('vstore/tb_product_type/Add.html?data=' + escape(JSON.stringify(selectTreeRow)), "分类", 520, 460, 'type');
				else {
					if(selectTreeRow.Flevel >= 1) {
						$.ligerDialog.warn("系统分类仅支持2级分类！");
						return;
					}
					var level = Number(selectTreeRow.level) + 1;
					commonDialog = parent.openCommWindow('vstore/tb_product_type/Add.html?pid=' + selectTreeRow.id + '&level=' + level, "分类", 520, 460, 'type');
				}
			}

			function putPhotos(photos, cmd) {
				if(null != commonDialog)
					commonDialog.frame.putPhotos(photos, cmd);
			}

			function saveInfo(row, cmd) {
				if(cmd == 'type') {
					var url = typeUrl + "&action=" + row.action;
					$api.ajax2(url, row.data, function(data) {
						initTree();
						f_reload();
					});
				} else if(cmd == 'spulist') {
					putProducts(row);
				}
			}

			function treeClick(node) {
				selectTreeRow = node.data;
				if(!selectTreeRow){
					ptid = node.data.id;
					f_reload();
				}
			}
			///////////////////////////////////////////////////////////////////
			function initGird(height) {
				var sender = "&key=";
				sender += "&ftypeid=0";
				var url = productUrl + sender;
				manager = $("#maingrid").ligerGrid({
					columns: [{
							display: '标题',
							name: 'title',
							align: 'left',
							width: 380,
							render: function(rowdata, rowindex, value) {
								var ret = "<img src=" + rowdata.Fhimg + " class='listimgs' style='float:left;top:5px;margin-right:5px' />";
								ret += "<div class='div-span-auto-line' style='margin-left:5px'>" + rowdata.Fsummary + "<br/>";

								ret += "<div>"
								return ret;

							}
						},
						{
							display: '销售状态',
							name: 'FisUp',
							width: 120,
							isSort: true,
							render: function(rowdata, rowindex, value) {
								if(rowdata.FisUp == 1) return '销售中';
								return '已下架'; 
							} 
						}, 
						{
							display: '货号',
							name: 'Fpcode',
							align: 'left',
							width: 180,
							isSort: true
						},
						{
							display: '条码',
							name: 'Fbarcode',
							align: 'left',
							width: 180
						},
						{
							display: '价格区间',
							name: 'Flowprice',
							isSort: true,
							width: 140,
							render: function(rowdata, rowindex, value) {
								return rowdata.Flowprice + "-" + rowdata.Fhighprice;

							}
						},
						{
							display: '原价',
							name: 'Foprice',
							isSort: true,
							width: 120,
							align: 'right'
						},
						{
							display: '售价',
							name: 'Fsaleprice',
							isSort: true,
							width: 120,
							align: 'left'
						},
						{
							display: '箱装数',
							name: 'Ft1',
							isSort: true,
							width: 120
						},
						{
							display: '大包装数',
							name: 'Ft2',
							isSort: true,
							width: 120
						},
						{
							display: '操作时间',
							name: 'Frefreshdt',
							isSort: true,
							minWidth: 220,
							render: function(rowdata, rowindex, value) {

								return $api.formatCTime(rowdata.Frefreshdt);

							}
						}
					],
					dataAction: 'local',
					url: url,
					method: 'get',
					width: '99.5%',
					height: height,
					rowHeight: 130,
					usePager: false, //是否分页  
					onDblClickRow: function(data, rowindex, rowobj) {
						edit(data);
					}
				});
			}

			function detail(i) {
				parent.openCommWindow('vstore/tb_product_type/SkuList.html?Fpid=' + i, "销售商品", 800, 600, 'skulist');

			}

			function setRate(i) {
				var row = manager.getRow(i);
				parent.openCommWindow2('vstore/tb_product_type/RateSetSpu.html?data=' + escape(JSON.stringify(row)),
					"分类商品", 500, 460,
					function(res) {
						var url = BaseUrl + "ProductTypeProduct?token=" + $api.getStorage("token") + "&action=edit";
						$api.ajaxapp2(url, res, function(data) {
							manager.reload(true);
						});

					});

			}

			function add() {
				if(selectTreeRow && selectTreeRow.level == 1) {
					parent.openCommWindow('vstore/tb_product_type/SpuSql.html', "商品选择", 800, 600, 'spulist');

				} else {
					$.ligerDialog.warn("请选择二级分类！");
				}

			}

			function putProducts(rows) {

				var arr = [];
				for(var i = 0; i < rows.length; i++) {
					var row = rows[i];
					arr.push(row.Fid);
				}
				var url = BaseUrl + "ProductTypeProduct";
				var param = {
					"Token": $api.getStorage("token"),
					"Rows": arr,
					"Ftypeid": selectTreeRow.id
				}

				$api.ajaxapp2(url, param, function(data) {
					f_reload();
				});
			}

			function edit(row) {
				parent.openCommWindow('vstore/tb_product_type/SkuList.html?Fpid=' + row.Fid, "销售商品", 800, 600, 'skulist');
			}

			function del(row) {
				if(row) {
					$.ligerDialog.confirm("确定要从该分类中移除商品吗?", function(yes) {
						if(yes) {
							var url = productUrl + "&action=del";

							$api.ajaxapp2(url, row, function(data) {
								f_reload();
							});
						}
					})
				} else {
					$.ligerDialog.warn("请选择商品！");
				}
			}

			function delp(i) {
				$.ligerDialog.confirm("确定要从该分类中移除商品吗?", function(yes) {
					if(yes) {
						var url = productUrl + "&action=del";
						var param = {
							"Fid": i
						}
						$api.ajaxapp2(url, param, function(data) {

							f_reload();
						});
					}
				});

			}

			function toolbar() {
				$api.getLimit(mid, 0, function(data) {
					$("#icon-list").append(data);
				});
			}

			function doSearch() {
				var key = $("#txtKeywords").val();
				var sender = "&key=" + key;
				sender += "&ftypeid=" + ptid;

				var url = productUrl + sender;
				manager.GetDataByURL(url);
			}

			function f_reload() {
				$("#txtKeywords").val("");
				doSearch();
			}
		</script>
	</head>
	<style type="text/css">
		.tree {
			width: 98%;
			height: 100%;
			border: 1px solid #d1d1d1;
			float: left;
			overflow: auto;
		}
	</style>

	<body class="mainbody">
		<form id="form1" onsubmit="return false" style="padding:0px;margin:0px;">
			<div class="alarm">
				友情提示：鼠标右键可操作自定义分类！系统分类显示在小程序分类栏目中，分类层级为2级，商品挂在第2级分类中。
			</div>
			<div style="margin:10px;">
				<div style="float:left;width:20%;height:100%">
					<div class="tree">
						<ul id="treediv" style="width:100%;height:100%;background:#ffffff"></ul>
					</div>
				</div>
				<div style="float:left;width:80%;height:100%"> 
				 
						<div class="toolbar">
							<div class="l-list"> 
								<ul id="icon-list" class="icon-list" style="margin-left:10px;">
									 
								</ul>
							</div>
							<div class="r-list" style="margin-left:10px;margin-right:10px;">

								<input type="text" ID="txtKeywords" placeholder='产品标题或货号条码' class="keyword" />
								<a class='btn-search' onclick='doSearch();'>搜索</a>

							</div>
						</div> 
					<div id="maingrid" style="margin: 0px;background:#fff" />
				</div>
			</div>

		</form>

	</body>

</html>